import NP from 'number-precision';
const cpos = (dp: number) => {
    const ar = new Array(dp);
    ar.unshift(1);
    return Number(ar.join('0'));
}

export default {
    /** 精确相减 */
    minus(num1: number, num2: number, dp?: number) {
        let value = NP.minus(num1, num2);
        if (dp) {
            const pos = cpos(dp);
            value = NP.divide(Math.floor(NP.times(value, pos)), pos);
        }
        return value;
    },
    /** 精确相加 */
    plus(num1: number, num2: number, dp?: number) {
        let value = NP.plus(num1, num2);
        if (dp) {
            const pos = cpos(dp);
            value = NP.divide(Math.floor(NP.times(value, pos)), pos);
        }
        return value;
    },

    /** 精确相除 */
    div(num1: number, num2: number, dp?: number) {
        let value = NP.divide(num1, num2);
        if (dp) {
            const pos = cpos(dp);
            value = NP.divide(Math.floor(NP.times(value, pos)), pos);
        }
        return value;
    },

    /** 精确相乘 */
    mul(num1: number, num2: number, dp?: number) {
        let value = NP.times(num1, num2);
        if (dp) {
            const pos = cpos(dp);
            value = NP.divide(Math.floor(NP.times(value, pos)), pos);
        }
        return value;
    }
}

